Skip to content

Refactor (packages/app/src/context/file/path.ts): Deeply nested control flow (level = 5) - #18

Open
RobbyTato wants to merge 3 commits into
CMU-17313Q:mainfrom
RobbyTato:refactor/13-unquoteGitPath-nested-conditional
Open

Refactor (packages/app/src/context/file/path.ts): Deeply nested control flow (level = 5)#18
RobbyTato wants to merge 3 commits into
CMU-17313Q:mainfrom
RobbyTato:refactor/13-unquoteGitPath-nested-conditional

Conversation

@RobbyTato

@RobbyTato RobbyTato commented Aug 31, 2026

Copy link
Copy Markdown

1. Issue

Link to the associated GitHub issue:
#13

Full path to the refactored file:
packages/app/src/context/file/path.ts

What do you think this file does?
This file provides path-handling utilities used to normalize, encode, and decode file paths across different operating systems (Linux, macOS, Windows) and different path representations (e.g., file:// URLs, git-quoted paths with escape sequences). It exposes helpers like createPathHelpers, encodeFilePath, decodeFilePath, stripQueryAndHash, stripFileProtocol, and unquoteGitPath, which together let the app consistently resolve a file's path relative to a workspace root regardless of platform-specific separators or encoding quirks.

What is the scope of your refactoring within that file?
I refactored the unquoteGitPath function. Specifically, I replaced a deeply nested ternary chain (checking next against "n", "r", "t", "b", "f", "v", "\", and """ one branch at a time) with a flat lookup table. I also identified and removed a dead/unreachable if branch in the octal-escape handling logic, since the surrounding condition guarantees the regex always matches.

Which Qlty‑reported issue did you address?
Deeply nested control flow (level = 5) at line 58

2. Refactoring

How did the specific issue you chose impact the codebase’s maintainability?
The nested ternary chain required tracking eight levels of conditionals to perform a simple character mapping. This made the escape logic unnecessarily slower and inflated the function's complexity score. The dead/unreachable if branch affected code coverage reports so it made sense to remove it along with solving this issue.

What changes did you make to resolve the issue?
Replaced the ternary chain with a flat ESCAPE_MAP lookup object and a single ESCAPE_MAP[next] ?? next fallback. Also removed an unreachable if (!match) branch in the octal-escape logic, since the preceding condition guarantees the regex always matches.

How do your changes improve maintainability? Did you consider alternatives?
The lookup table removes all nesting for the escape mapping and is easier to extend (one line per new escape). Removing the dead branch further reduces complexity. I considered a switch statement, but it wouldn't reduce branching as much as a map.

3. Validation

How did you validate that the change is correct?
Added tests in path.test.ts under describe("unquoteGitPath", ...) covering all ESCAPE_MAP entries, octal escapes (1–3 digits, multiple in one string), unrecognized escape fallback, unquoted/malformed input, and trailing backslash. Ran bun test --coverage --coverage-dir=./coverage and confirmed all tests pass and the changed lines are covered.

Attach a screenshot of the test coverage showing the lines were executed by the tests.
image
Local linter and tests running:
image

Attach a screenshot showing the tests that cover the change passing during CI
image

Attach a screenshot of qlty smells --no-snippets <full/path/to/file.ts> showing fewer reported issues after the changes.
Before:
image
After:
image

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant